//
//  VoiceBookViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/15.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "VoiceBookViewController.h"
#import "BookCollectCell.h"
#import "FooterCollectionReusableView.h"
#import "VideoDetailVC.h"
#import "BookDetailModel.h"
#import "MediaPlayModel.h"
#import "MediaListVC.h"
#import "ClassifyHeaderView.h"
#import "VideoClassifyListVC.h"

@interface VoiceBookViewController () <UICollectionViewDelegate,UICollectionViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource, ClassifyHeaderViewDelegate>

@property (nonatomic, strong)UICollectionView *bookCollectionView;
//数据源
@property (nonatomic, strong)NSArray<VideoClassifyModel *> *dataArray;

@end

@implementation VoiceBookViewController
{
    BOOL requestCompleted;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    requestCompleted = NO;
    [self getNetData];
    [self buildUI];
}

- (void)getNetData {
    [CXDNetHelp getRequestWithURLString:@"http://www.wenqujingdian.com/index.php/jiekou/getVideoClass" parameters:nil success:^(id  _Nullable responseObject) {
        requestCompleted = YES;
        [self.bookCollectionView reloadEmptyDataSet];
        self.dataArray = [VideoClassifyModel mj_objectArrayWithKeyValuesArray:responseObject];
        [self.bookCollectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        requestCompleted = YES;
        [self.bookCollectionView reloadEmptyDataSet];
    }];
}

- (void)buildUI {
    [self.view addSubview:self.bookCollectionView];
    self.bookCollectionView.emptyDataSetDelegate = self;
    self.bookCollectionView.emptyDataSetSource = self;
}

#pragma mark - 懒加载
- (UICollectionView *)bookCollectionView {
    if (!_bookCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth) / 3, 150);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.headerReferenceSize = CGSizeMake(kScreenWidth, 45);
        _bookCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - UIApplication.sharedApplication.statusBarFrame.size.height) collectionViewLayout:layout];
        _bookCollectionView.delegate = self;
        _bookCollectionView.dataSource = self;
        _bookCollectionView.backgroundColor = RGB(238, 238, 238);
        _bookCollectionView.showsVerticalScrollIndicator = NO;
//        _bookCollectionView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
        [_bookCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
        [_bookCollectionView registerClass:ClassifyHeaderView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _bookCollectionView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray[section].childs.count > 3? 3: self.dataArray[section].childs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    MediaPlayModel *tempModel = self.dataArray[indexPath.section].childs[indexPath.item];
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootHomeURL,tempModel.spic]]];
    cell.nameLabel.text = tempModel.news_tit;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MediaListVC *listVC = [[MediaListVC alloc] init];
    MediaPlayModel *dataModel = self.dataArray[indexPath.section].childs[indexPath.item];
    listVC.navigationItem.title = dataModel.news_tit;
    listVC.dataArray = dataModel.mulu;
    [self.navigationController pushViewController:listVC animated:YES];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        ClassifyHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.delegate = self;
        header.classifyTitle = self.dataArray[indexPath.section].name;
        header.section = indexPath.section;
        return header;
    }
    return nil;
}

- (void)classifyHeaderView:(ClassifyHeaderView *)headerView moreBtnDidClick:(UIButton *)button atSection:(NSInteger)section {
    
    VideoClassifyListVC *vc = [[VideoClassifyListVC alloc] init];
    vc.mediaId = self.dataArray[section].id;
    vc.mediaName =  self.dataArray[section].name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    
    return requestCompleted && self.dataArray.count == 0;
}

@end
